//
//  MBBaseController.m
//  mibaotu
//
//  Created by 彭康 on 16/9/14.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBBaseController.h"
#import "MBSongDetailController.h"
#import "MBDetailViewController.h"
#import "MBSpaceMyParentChildShowPlayViewController.h"


#import "MBDetailViewForIpadController.h"
#import "MBSpaceMyParentChildShowPlayViewForIpadController.h"
#import "MBSongDetailForIpadController.h"

#import <UMMobClick/MobClick.h>

@interface MBBaseController () {
    UIImageView *_liveImgView;
    NSString    *_pageName;
}


@end

@implementation MBBaseController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    if (_liveImgView) {
        [self onLoadPlayerAnimation];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onPlayMusicForAudio) name:@"AUDIO_PLAYER" object:nil];
    }
    if (_pageName.length > 0) {
        [MobClick beginLogPageView:_pageName];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if (_pageName.length > 0) {
        [MobClick endLogPageView:_pageName];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromHex(0xf5f5f5);
    self.orientation = [UIDevice currentDevice].orientation;
    _pageName = [NSString stringWithUTF8String:object_getClassName(self.class)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)setBackItem
{
    UIImage *img = [UIImage imageNamed:@"fanhui"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(5, 0, img.size.width, img.size.height);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [btn setImage:img forState:UIControlStateNormal];
    [btn setImage:img forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [self setLeftBarItem:btn];
    
}

- (void)setRightPlayerItem
{
    AVPlayer *player = (AVPlayer *)[UserManager standerDefult].mainPlayer;
    NSMutableArray *mutArr = [[NSMutableArray alloc]init];
    for (int i=1; i<=4; i++) {
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"bofang1-%d",i]];
        [mutArr addObject:img];
    }
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt addTarget:self action:@selector(onPlayMusic) forControlEvents:UIControlEventTouchUpInside];
    
    UIImage *img = [UIImage imageNamed:@"bofang1-1"];
    _liveImgView = [[UIImageView alloc]initWithFrame:CGRectMake((bt.width-img.size.width), (bt.height-img.size.height)/2., img.size.width, img.size.height)];
    _liveImgView.backgroundColor = [UIColor clearColor];
    _liveImgView.image = img;
    _liveImgView.animationImages = mutArr;
    _liveImgView.animationDuration = .6;
    _liveImgView.animationRepeatCount = MAXFLOAT;
    
    [bt addSubview:_liveImgView];
    
    if (player) {
        if ([UserManager standerDefult].isPlaying) {
            [_liveImgView startAnimating];
        } else {
            [_liveImgView stopAnimating];
        }
    } else {
        [_liveImgView stopAnimating];
    }
    
    [self setRightBarItems:bt];
}

//播放图片动画
- (void)onLoadPlayerAnimation
{
    AVPlayer *player = (AVPlayer *)[UserManager standerDefult].mainPlayer;
    if (player) {
        if ([UserManager standerDefult].isPlaying) {
            [_liveImgView startAnimating];
        } else {
            [_liveImgView stopAnimating];
        }
    } else {
        [_liveImgView stopAnimating];
    }
}

- (void)onPlayMusicForAudio
{
    [self onLoadPlayerAnimation];
}

- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setLeftBarItem:(UIView *)view
{

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -6;
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItems = @[spacer,leftItem];

}

- (void)setRightBarItems:(UIView *)view
{
    //    解决系统默认占位
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -6;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.rightBarButtonItems = @[spacer,rightItem];
}

- (void)setTitleView:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = title;
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    [label sizeToFit];
    self.navigationItem.titleView = label;
}

- (void)setTitleText:(NSString *)title Color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = title;
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = color;
    label.backgroundColor = [UIColor clearColor];
    [label sizeToFit];
    self.navigationItem.titleView = label;
}

- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    _orientation = orientation;
    [self onUpdateUIForView:orientation];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    
}

//播放音乐
- (void)onPlayMusic
{
    [self onLoadPlayerAnimation];
    
    if ([UserManager standerDefult].audioPlayerType == AudioPLayerType_YinPin) {
        
        if (ISIPAD) {
            MBSongDetailForIpadController*songDetailVC=[[MBSongDetailForIpadController alloc]init];
            songDetailVC.isFromOnLine = [UserManager standerDefult].isOnlinePlayer;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }
        
        MBSongDetailController *songDetailVC = [[MBSongDetailController alloc] init];
        songDetailVC.isFromOnLine = [UserManager standerDefult].isOnlinePlayer;
        [self.navigationController pushViewController:songDetailVC animated:YES];
    }
    else if ([UserManager standerDefult].audioPlayerType == AudioPLayerType_Qinzix) {
        if (ISIPAD) {
            MBDetailViewForIpadController*songDetailVC=[[MBDetailViewForIpadController alloc]init];
            songDetailVC.dataAllArr = [UserManager standerDefult].qinzixiuArr;
            songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }
        
        
        MBDetailViewController *songDetailVC = [[MBDetailViewController alloc] init];
        songDetailVC.dataAllArr = [UserManager standerDefult].qinzixiuArr;
        songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
        [self.navigationController pushViewController:songDetailVC animated:YES];
    }
    else if ([UserManager standerDefult].audioPlayerType == AudioPLayerType_ZhuBo) {
        
        if (ISIPAD) {
            MBSpaceMyParentChildShowPlayViewForIpadController*songDetailVC=[[MBSpaceMyParentChildShowPlayViewForIpadController alloc]init];
            songDetailVC.dataDic = [UserManager standerDefult].zhuboDic;
            songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }
        MBSpaceMyParentChildShowPlayViewController *songDetailVC = [[MBSpaceMyParentChildShowPlayViewController alloc] init];
        songDetailVC.dataDic = [UserManager standerDefult].zhuboDic;
        songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
        [self.navigationController pushViewController:songDetailVC animated:YES];
    }
}

- (UIImage *)getCenterImg:(UIImage *)image withImgView:(UIImageView *)_bgImgView
{
    CGImageRef imageRef = image.CGImage;
    CGFloat scale = 1. ;//* [UIScreen mainScreen].scale / image.scale;
    float imgHeight = 1.0 * image.size.width * _bgImgView.height/_bgImgView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top*scale, image.size.width*scale, rectHeight*scale);
    } else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * _bgImgView.width/_bgImgView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left*scale, 0, rectWidth*scale, image.size.height*scale);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}

#pragma mark - 旋转方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (ISIPAD == NO) {
        return toInterfaceOrientation == UIDeviceOrientationPortrait;
    }
    return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotate
{
    if (ISIPAD) {
    return YES;
}else{
    return NO;
}
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (ISIPAD == NO) {
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@end
